﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class OutStoreOrderProcessor : BaseProcessor {

        private int _pageSize;
        private OutStoreOrderRepository _outStoreOrderRepository;

        public OutStoreOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._outStoreOrderRepository = new OutStoreOrderRepository(pageSize);
        }

        public OutStoreOrder FetchOutStoreOrderById(int outStoreOrderId) {
            try {
                return this._outStoreOrderRepository.FetchOutStoreOrderById(outStoreOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定出库单失败！");
            }
        }

        public List<OutStoreOrderDetailsViewModel> FetchOutStoreOrderDetailsViewModelList(int outStoreOrderId) {
            try {
                return this._outStoreOrderRepository.FetchOutStoreOrderByIdForView(outStoreOrderId).OutStoreOrderDetails.Select(x => {
                    return new OutStoreOrderDetailsViewModel() {
                        Key = x.OutStoreOrderDetailsId,
                        MaterialCode = x.Material.Code,
                        Quantity = x.Quantity,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定出库单失败！");
            }
        }

        public List<OutStoreOrderViewModel> FetchOutStoreOrderViewModelList(OutStoreOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<OutStoreOrder> resultList = this._outStoreOrderRepository.FetchOutStoreOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new OutStoreOrderViewModel() {
                        Key = x.OutStoreOrderId,
                        Code = x.Code,
                        OutStoreType = x.Basic.Code,
                        ProduceConductCode = x.ProduceConduct.Code,
                        ProductUnitCode = x.Basic1.Code,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取出库单列表失败！");
            }
        }

        public void Insert(OutStoreOrder outStoreOrder) {
            try {
                outStoreOrder.OutStoreOrderId = Guid.NewGuid().GetHashCode();
                outStoreOrder.CreatedDatetime = DateTime.Now;
                outStoreOrder.OutStoreOrderDetails.ToList().ForEach(x => {
                    x.CreatedBy = outStoreOrder.CreatedBy;
                    x.CreatedDatetime = outStoreOrder.CreatedDatetime;
                    x.OutStoreOrderDetailsId = Guid.NewGuid().GetHashCode();
                    x.OutStoreOrderId = outStoreOrder.OutStoreOrderId;
                });
                this._outStoreOrderRepository.InsertOutStoreOrder(outStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新出库单失败！");
            }
        }

        public void Update(OutStoreOrder outStoreOrder) {
            try {
                OutStoreOrder oldOutStoreOrder = this.FetchOutStoreOrderById(outStoreOrder.OutStoreOrderId);
                if (!oldOutStoreOrder.RowVersion.SequenceEqual(outStoreOrder.RowVersion)) {
                    throw this.BuildException("更新出库单失败，数据不一致性错误！");
                }
                outStoreOrder.ModifiedDatetime = DateTime.Now;
                this._outStoreOrderRepository.UpdateOutStoreOrder(outStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新出库单失败！");
            }
        }

        public void Delete(OutStoreOrder outStoreOrder) {
            try {
                this._outStoreOrderRepository.Delete(outStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除出库单失败！");
            }
        }

        public void Delete(params int[] outStoreOrderIds) {
            try {
                this._outStoreOrderRepository.DeleteMultipleOutStoreOrder(outStoreOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除出库单失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._outStoreOrderRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取出库单最大流水号失败！");
            }
        }
    }
}
